package com.xc.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xc.pojo.Role;
import com.xc.service.RoleService;
import com.xc.service.SysUserService;
import com.xc.utils.DataGridViewResult;
import com.xc.utils.SysConstants;
import com.xc.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/admin/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private SysUserService userService;
    /**
     * 获取角色列表
     * @param roleVo
     * @return
     */
    @RequestMapping("/list")
    public DataGridViewResult list(RoleVo roleVo){
        PageHelper.startPage(roleVo.getPage(),roleVo.getLimit());
        List<Role> roleList = roleService.queryRoleList(roleVo);
        PageInfo<Role> pageInfo=new PageInfo<Role>(roleList);
        return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 添加角色
     * @param role
     * @return
     */
    @RequestMapping("/addRole")
    public String addRole(Role role){
        HashMap<String, Object> map = new HashMap<String, Object>();
        if (roleService.addRole(role)>0){
            map.put(SysConstants.SUCCESS,true);
            map.put(SysConstants.MESSAGE,"添加成功");
        }else {
            map.put(SysConstants.SUCCESS,false);
            map.put(SysConstants.MESSAGE,"添加失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 修改角色
     * @param role
     * @return
     */
    @RequestMapping("/updateRole")
    public String updateRole(Role role){
        HashMap<String, Object> map = new HashMap<String, Object>();
        if (roleService.updateRole(role)>0){
            map.put(SysConstants.SUCCESS,true);
            map.put(SysConstants.MESSAGE,"修改成功");
        }else {
            map.put(SysConstants.SUCCESS,false);
            map.put(SysConstants.MESSAGE,"修改失败");
        }
        return JSON.toJSONString(map);
    }
    /**
     * 根据角色编号查询该角色下的用户的数量
     * @return
     */
    @RequestMapping("/checkRoleUser")
    public String checkRoleUser(Integer roleId){
        HashMap<String, Object> map = new HashMap<String, Object>();
        if (userService.getUserByRoleId(roleId)>0){
            map.put(SysConstants.EXIST,true);
            map.put(SysConstants.MESSAGE,"该角色存在用户，无法删除");
        }else {
            map.put(SysConstants.EXIST,false);
        }
        return JSON.toJSONString(map);
    }
    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @RequestMapping("/deleteRole")
    public String deleteDept(Integer roleId){
        HashMap<String, Object> map = new HashMap<String, Object>();
        if (roleService.deleteRole(roleId)>0){
            map.put(SysConstants.SUCCESS,true);
            map.put(SysConstants.MESSAGE,"删除成功");
        }else {
            map.put(SysConstants.SUCCESS,false);
            map.put(SysConstants.MESSAGE,"删除失败");
        }
        return JSON.toJSONString(map);
    }
}
